class MySingleList {
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;

    public void create() {
        ListNode node1 = new ListNode(6);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(8);
        /*ListNode node4 = new ListNode(59);
        ListNode node5 = new ListNode(7);*/
        node1.next = node2;
        node2.next = node3;
        /*node3.next = node4;
        node4.next = node5;*/
        head = node1;
    }

    public static void main(String[] args) {
        MySingleList mySingleList = new MySingleList();
        mySingleList.create();
        mySingleList.partition(9);
    }

    public boolean chkPalindrome(ListNode A) {
        // write code here
        ListNode slow = A;
        ListNode fast = A;
        // 先找到中间节点
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        // 逆置
        ListNode cur = slow.next;

        while(cur != null) {
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        // 判断是否是回文串
        ListNode head = A;
        // 奇数节点情况下
        while(head != slow) {
            if(head.val != slow.val) {
                return false;
            }
            // 偶数节点情况下
            if(head.next == slow) {
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;


    }

    public MySingleList.ListNode partition(int x) {
        // write code here
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        ListNode cur = head;
        while(cur != null) {
            if(cur.val < x) {
                if(bs == null) {
                    bs = cur;
                    be = cur;
                }else {
                    be.next = cur;
                    be = cur;
                }

            }else {
                if(as == null) {
                    as = cur;
                    ae = cur;
                }else {
                    ae.next = cur;
                    ae = cur;
                }

            }
            cur = cur.next;
        }
        // 如果第一个段空 直接返回第二个
        if(bs == null) {
            return as;
        }
        be.next = as;
        if(as != null) {
            ae.next = null;
        }

        return bs;
    }


    public MySingleList.ListNode mergeTwoLists(MySingleList.ListNode list1, MySingleList.ListNode list2) {
        if(list1 == null && list2 != null) {
            return list2;
        }
        if(list1 != null && list2 == null) {
            return list1;
        }
        if(list1 == null && list2 == null) {
            return null;
        }
        MySingleList.ListNode newHead = new MySingleList.ListNode(-1);
        MySingleList.ListNode head = newHead;
        while(list1 != null && list2 != null ) {
            if(list1.val < list2.val) {
                head.next = list1;
                head = list1;
                list1 = list1.next;
            }else {
                head.next = list2;
                head = list2;
                list2 = list2.next;
            }
        }
        if(list1 == null) {
            head.next = list2;
        }
        if(list2 == null) {
            head.next = list1;
        }
        return newHead.next;
    }

    public MySingleList.ListNode findKthToTail(MySingleList.ListNode head, int k) {
        if(k <= 0) {
            return null;
        }
        MySingleList.ListNode fast = head;
        MySingleList.ListNode slow = head;
        //1. 先让fast指针先走
        while(k - 1 != 0) {
            fast = fast.next;
            if(fast == null) {
                return null;
            }
            k--;
        }
        //2. 然后fast和slow一起走
        while(fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
    public MySingleList.ListNode middleNode(MySingleList.ListNode head) {
        if(head == null) {
            return null;
        }
        if(head.next == null) {
            return head;
        }
        MySingleList.ListNode fast = head;
        MySingleList.ListNode slow = fast;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    public MySingleList.ListNode removeElements(MySingleList.ListNode head, int val) {
        if(head == null) {
            return null;
        }

        MySingleList.ListNode prev = head;
        MySingleList.ListNode cur = head.next;
        while(cur != null) {
            if(cur.val == val) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = prev.next;
                cur = cur.next;
            }
        }
        if(head.val == val) {
            head = head.next;
        }
        return head;
    }

    public MySingleList.ListNode reverseList(MySingleList.ListNode head) {
        if(head == null) {
            return null;
        }
        MySingleList.ListNode cur = head.next;
        head.next = null;
        while(cur != null) {
            MySingleList.ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }
}
